Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
resampler.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Roc authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_audio/resampler.h
10//! @brief Resampler.
11
12#ifndef ROC_AUDIO_RESAMPLER_H_
13#define ROC_AUDIO_RESAMPLER_H_
14
15#include "roc_audio/frame.h"
16#include "roc_audio/ireader.h"
17#include "roc_audio/units.h"
18#include "roc_core/array.h"
20#include "roc_core/slice.h"
21#include "roc_core/stddefs.h"
22#include "roc_packet/units.h"
23
24namespace roc {
25namespace audio {
26
27//! Resampler parameters.
29 //! Sinc table precision.
30 //! @remarks
31 //! Affects sync table size.
32 //! Lower values give lower quality but rarer cache misses.
34
35 //! Resampler internal window length.
36 //! @remarks
37 //! Affects sync table size and number of CPU cycles.
38 //! Lower values give lower quality but higher speed and also rarer cache misses.
40
42 : window_interp(128)
43 , window_size(32) {
44 }
45};
46
47//! Resamples audio stream with non-integer dynamically changing factor.
48class Resampler : public core::NonCopyable<> {
49public:
50 //! Initialize.
52 const ResamplerConfig& config,
54 size_t frame_size);
55
56 //! Check if object is successfully constructed.
57 bool valid() const;
58
59 //! Set new resample factor.
60 //! @remarks
61 //! Resampling algorithm needs some window of input samples. The length of the window
62 //! (length of sinc impulse response) is a compromise between SNR and speed. It
63 //! depends on current resampling factor. So we choose length of input buffers to let
64 //! it handle maximum length of input. If new scaling factor breaks equation this
65 //! function returns false.
66 bool set_scaling(float);
67
68 //! Resamples the whole output frame.
69 bool resample_buff(Frame& out);
70
71 //! Push new buffer on the front of the internal FIFO, which comprisesthree window_.
75
76private:
77 typedef uint32_t fixedpoint_t;
78 typedef uint64_t long_fixedpoint_t;
79 typedef int32_t signed_fixedpoint_t;
80 typedef int64_t signed_long_fixedpoint_t;
81
82 const packet::channel_mask_t channel_mask_;
83 const size_t channels_num_;
84
85 inline size_t channelize_index(const size_t i, const size_t ch_offset) const {
86 return i * channels_num_ + ch_offset;
87 }
88
89 //! Computes single sample of the particular audio channel.
90 //!
91 //! @param channel_offset a serial number of the channel
92 //! (e.g. left -- 0, right -- 1, etc.).
93 sample_t resample_(size_t channel_offset);
94
95 bool check_config_() const;
96
97 bool fill_sinc_();
98 sample_t sinc_(fixedpoint_t x, float fract_x);
99
100 sample_t* prev_frame_;
101 sample_t* curr_frame_;
102 sample_t* next_frame_;
103
104 size_t out_frame_pos_;
105
106 float scaling_;
107
108 const size_t frame_size_;
109 const size_t frame_size_ch_;
110
111 const size_t window_size_;
112 const fixedpoint_t qt_half_sinc_window_size_;
113
114 const size_t window_interp_;
115 const size_t window_interp_bits_;
116
117 core::Array<sample_t> sinc_table_;
118 const sample_t* sinc_table_ptr_;
119
120 // half window len in Q8.24 in terms of input signal
121 fixedpoint_t qt_half_window_size_;
122 const fixedpoint_t qt_epsilon_;
123
124 const fixedpoint_t qt_frame_size_;
125
126 // time position of output sample in terms of input samples indexes
127 // for example 0 -- time position of first sample in curr_frame_
128 fixedpoint_t qt_sample_;
129
130 // time distance between two output samples, equals to resampling factor
131 fixedpoint_t qt_dt_;
132
133 // the step with which we iterate over the sinc_table_
134 fixedpoint_t qt_sinc_step_;
135
136 const sample_t cutoff_freq_;
137
138 bool valid_;
139};
140
141} // namespace audio
142} // namespace roc
143
144#endif // ROC_AUDIO_RESAMPLER_H_
Dynamic array.
Audio frame.
Definition: frame.h:22
Resamples audio stream with non-integer dynamically changing factor.
Definition: resampler.h:48
bool resample_buff(Frame &out)
Resamples the whole output frame.
Resampler(core::IAllocator &allocator, const ResamplerConfig &config, packet::channel_mask_t channels, size_t frame_size)
Initialize.
bool set_scaling(float)
Set new resample factor.
void renew_buffers(core::Slice< sample_t > &prev, core::Slice< sample_t > &cur, core::Slice< sample_t > &next)
Push new buffer on the front of the internal FIFO, which comprisesthree window_.
bool valid() const
Check if object is successfully constructed.
Dynamic array.
Definition: array.h:25
Memory allocator interface.
Definition: iallocator.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Slice.
Definition: slice.h:23
Audio frame.
float sample_t
Audio sample.
Definition: units.h:21
uint32_t channel_mask_t
Bitmask of channels present in audio packet.
Definition: units.h:77
Root namespace.
Non-copyable object.
Audio reader interface.
Various units used in audio processing.
Various units used in packets.
Slice.
Commonly used types and functions.
Resampler parameters.
Definition: resampler.h:28
size_t window_size
Resampler internal window length.
Definition: resampler.h:39
size_t window_interp
Sinc table precision.
Definition: resampler.h:33